
var setting = {
    check: {
        enable: true
    },
    view: {
        dblClickExpand: false,
        showLine: true,
        selectedMulti: false,
        showIcon: false    //去掉图标样式
    },
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: ""
        }
    },
    callback: {
        onClick: function (e, treeId, treeNode) {
            zTree.checkNode(treeNode, !treeNode.checked, true);
        }
    }
};

/* 获取菜单树 */
$(document).ready(function () {
    let roleId = $("#roleId").val();
    $.http.get("/base/role/menu/zTree/" + roleId, function (res) {
        zTree = $.fn.zTree.init($("#tree"), setting, res.data);
        sitFolderTree = $.fn.zTree.getZTreeObj("tree");
        sitFolderTree.expandAll(true);
    });
});

//获取checkbox的值
layui.use('form', function () {
    form = layui.form;
    form.on('submit(sub)', function (data) {
        let menuIds = getCheckedTreeNodes();
        let roleId = $("#roleId").val();
        let subData = data.field;
        subData.menuId = menuIds;
        subData.roleId = roleId;
        $.http.post("/base/role/menu", data.field, function (res) {
            $.msg.msgOkModel(res.msg);
        });
        return false;
    });

    form.on('submit(close)', function () {
        let index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
});

function getCheckedTreeNodes() {
    var treeObj = $.fn.zTree.getZTreeObj("tree");
    var nodes = treeObj.getCheckedNodes(true);
    var roleCodes = new Array();
    for (var i = 0; i < nodes.length; i++) {
        roleCodes[i] = nodes[i].id;
    }
    return roleCodes.join(",");
}